package cn.com.ddapp.ddm.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.pay.wx.Constants;
import cn.com.ddapp.ddm.polling.PollingService;


public class WXPayEntryActivity extends Activity implements
        IWXAPIEventHandler {

    public static final String TAG = "WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//		UIHelper.ToastMessage(getApplicationContext(), "微信支付-huidaio");
        api = WXAPIFactory.createWXAPI(this, Constants.WXAppID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);
//		UIHelper.ToastMessage(getApplicationContext(), "微信支付-huidaio");
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            // AlertDialog.Builder builder = new AlertDialog.Builder(this);
            // builder.setTitle("提示");
            // builder.setMessage("支付结果：" + resp.errStr + ";code="
            // + String.valueOf(resp.errCode));
            // builder.show();
            /**
             * resp.errCode== 0 ：表示支付成功 resp.errCode== -1 ：表示支付失败 resp.errCode==
             * -2 ：表示取消支付
             */
            String str = "";
            switch (resp.errCode) {
                case 0:
                    System.out.println("支付成功广播");
                    BaseApplication.showToast(R.string.pay_success);
                    Intent intent1 = new Intent();
                    intent1.setAction("com.ryantang.service.PollingService");
                    intent1.putExtra("status", "2");
                    sendBroadcast(intent1);
                    finish();
                    break;
                case -1:
                    BaseApplication.showToast(R.string.pay_failed);
//				UIHelper.ToastMessage(this, str);
                    break;
                case -2:
//				str="支付";
                    break;
            }

//			myApplication.showAlertDailog(
//					WXPayEntryActivity.this,
//					"提示",
//					"支付结果：" + resp.errStr + ";code="
//							+ String.valueOf(resp.errCode), true, false,
//					new DialogInterface.OnClickListener() {
//
//						@Override
//						public void onClick(DialogInterface dialog, int which) {
//							// TODO Auto-generated method stub
//							appManager.finishActivity(PayForAppActivity.class);
//						}
//					}, null);
        }
        finish();
    }
}